package com.example.administrator.baikangxing.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.baikangxing.R;
import com.example.administrator.baikangxing.activity.HeartActivity;
import com.example.administrator.baikangxing.activity.OxygenActivity;
import com.example.administrator.baikangxing.activity.PressureActivity;
import com.example.administrator.baikangxing.activity.SleepActivity;
import com.example.administrator.baikangxing.activity.StepActivity;
import com.example.administrator.baikangxing.bean.HomeInfo;
import com.example.administrator.baikangxing.config.Constants;
import com.example.administrator.baikangxing.utils.ColorFontTextView;
import com.example.administrator.baikangxing.utils.TimeUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * A simple {@link Fragment} subclass.
 */
public class MeterFragment extends BaseFragment implements View.OnClickListener {

    private LinearLayout meter_oxy, meter_heart, meter_step, meter_sleep;
    private TextView meter_tv_heartRate;
    private ColorFontTextView meter_tv_lowPresure;
    private ColorFontTextView meter_tv_highPresure;
    private ColorFontTextView meter_tv_sleepTime;
    private ColorFontTextView meter_tv_deepSleepTime;
    private TextView meter_tv_bloodOxygen;
    private TextView meter_tv_passometer;
    private String sleepTime;
    private String deepSleepTime;
    private LinearLayout meter_ll_pressure;
    private HomeInfo data;

    @Override
    public int getContentViewLayoutId() {
        return R.layout.fragment_meter;
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {//加上判断
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void initView() {
        meter_oxy = findView(R.id.meter_ll_oxy);
        meter_heart = findView(R.id.meter_ll_heart);
        meter_sleep = findView(R.id.meter_ll_sleep);
        meter_step = findView(R.id.meter_ll_step);
        meter_tv_heartRate = findView(R.id.meter_tv_heartRate);
        meter_tv_lowPresure = findView(R.id.meter_tv_lowPresure);
        meter_tv_highPresure = findView(R.id.meter_tv_highPresure);
        meter_tv_sleepTime = findView(R.id.meter_tv_sleepTime);
        meter_tv_deepSleepTime = findView(R.id.meter_tv_deepSleepTime);
        meter_tv_bloodOxygen = findView(R.id.meter_tv_bloodOxygen);
        meter_tv_passometer = findView(R.id.meter_tv_passometer);
        meter_ll_pressure = findView(R.id.meter_ll_pressure);
        meter_sleep.setOnClickListener(this);
        meter_step.setOnClickListener(this);
        meter_oxy.setOnClickListener(this);
        meter_heart.setOnClickListener(this);
        meter_ll_pressure.setOnClickListener(this);
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getEventBus(HomeInfo homeInfo) {
        data = homeInfo;
        meter_tv_heartRate.setText(homeInfo.getHeartrate());
        meter_tv_lowPresure.setText("低压："+homeInfo.getLowpresure()+"mmHg");
        meter_tv_highPresure.setText("高压："+homeInfo.getHighpresure()+"mmHg");
        meter_tv_lowPresure.setTextStyle("低压："+homeInfo.getLowpresure()+"mmHg","低压："+"|"+homeInfo.getLowpresure()+"|"+"mmHg","11|15|11");
        meter_tv_highPresure.setTextStyle("高压："+homeInfo.getHighpresure()+"mmHg","高压：|"+homeInfo.getHighpresure()+"|mmHg","11|15|11");
        sleepTime = homeInfo.getSleeptime();
        deepSleepTime = homeInfo.getDeepsleeptime();
        int[] sleep = TimeUtils.minTohours(sleepTime);
        int[] deep = TimeUtils.minTohours(deepSleepTime);
        meter_tv_sleepTime.setTextStyle("睡眠时间："+ sleep[0]+"小时"+ sleep[1]+"分","睡眠时间：|"+ sleep[0]+"|小时|"+ sleep[1]+"|分","11|15|11|15|11");
        meter_tv_deepSleepTime.setTextStyle("深睡时间："+ deep[0]+"小时"+ deep[1]+"分","深睡时间：|"+ deep[0]+"|小时|"+ deep[1]+"|分","11|15|11|15|11");
        meter_tv_bloodOxygen.setText(homeInfo.getBloodoxygen());
        meter_tv_passometer.setText(homeInfo.getPassometer());
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.meter_ll_oxy://血氧
                Intent intent = new Intent(getActivity(), OxygenActivity.class);
                intent.putExtra(Constants.HEALTH_TARGET[2],meter_tv_bloodOxygen.getText().toString().trim());
                startActivity(intent);
                break;
            case R.id.meter_ll_heart://心率
                Intent intent1 = new Intent(getActivity(), HeartActivity.class);
                intent1.putExtra(Constants.HEALTH_TARGET[0],meter_tv_heartRate.getText().toString().trim());
                startActivity(intent1);
                break;
            case R.id.meter_ll_sleep://睡眠
                Intent intent2 = new Intent(getActivity(), SleepActivity.class);
                intent2.putExtra(Constants.HEALTH_TARGET[5]+"sleep",sleepTime);
                intent2.putExtra(Constants.HEALTH_TARGET[5]+"deep",deepSleepTime);
                startActivity(intent2);
                break;
            case R.id.meter_ll_step://计步
                Intent intent3 = new Intent(getActivity(), StepActivity.class);
                intent3.putExtra(Constants.HEALTH_TARGET[4],meter_tv_passometer.getText().toString().trim());
                startActivity(intent3);
                break;
            case R.id.meter_ll_pressure://血压
                Intent intent4 = new Intent(getActivity(), PressureActivity.class);
                intent4.putExtra(Constants.LOW_PRESSURE,data.getLowpresure());
                intent4.putExtra(Constants.HIGH_PRESSURE,data.getHighpresure());
                startActivity(intent4);
                break;
            default:
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }//加上判断
    }
}
